package cn.baage.campus.service.impl;

import cn.baage.campus.enums.ExceptionType;
import cn.baage.campus.exception.BizException;
import cn.baage.campus.mapper.UserMapper;
import cn.baage.campus.model.dto.LoginUser;
import cn.baage.campus.model.po.User;
import cn.baage.campus.model.dto.UserRoleDTO;
import cn.baage.campus.service.UserService;
import cn.baage.campus.utils.MinioUtils;
import cn.baage.campus.utils.SecurityUtils;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: 用户Service实现
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final MinioUtils minioUtils;

    /**
     * 根据用户名获取用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public UserRoleDTO getAuthInfoByUserName(String username) {
        return this.baseMapper.selectRoleByUserName(username);
    }


    /**
     * 根据微信ID获取用户
     *
     * @param wxId 用户名
     * @return 用户信息
     */
    @Override
    public UserRoleDTO getAuthInfoByWx(String wxId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getWxId, wxId);
        User userById = this.baseMapper.selectOne(queryWrapper);
        if (userById == null) {
            return null;
        }
        return getAuthInfoByUserName(userById.getUsername());
    }


    @Override
    public User getLoginUserInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        User user = this.baseMapper.selectById(loginUser.getId());
        user.setPassword("");
        user.setAvatarUrl(minioUtils.getAccessUrl(user.getAvatarUrl()));
        return user;
    }


    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public User getByUserName(String username) {
        User user = new User();
        user.setUsername(username);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }


    /**
     * 根据邮箱获取用户信息
     *
     * @param email 邮箱
     * @return 用户信息
     */
    @Override
    public User getByEmail(String email) {
        User user = new User();
        user.setEmail(email);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getEmail, email);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }


    /**
     * 更新用户信息
     *
     * @param user 用户信息
     */
    @Override
    public void updateUserInfo(User user) {
        user.setUpdateTime(new Date());
        this.baseMapper.updateById(user);
    }


    /**
     * 更新个人信息
     *
     * @param user 更新信息
     */
    @Override
    public void updateLoginInfo(User user) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        User byId = getById(loginUser.getId());
        if (StrUtil.isNotBlank(user.getNickName())) {
            byId.setNickName(user.getNickName());
        }
        if (StrUtil.isNotBlank(user.getAvatarUrl())) {
            byId.setAvatarUrl(user.getAvatarUrl());
        }
        updateUserInfo(byId);
    }


}
